#include "handle.h"
#include <math.h>
#include <iostream>
#define PI (3.1416)
Handle::Handle(QWidget *parent)
    : QWidget(parent)
{
   // setAutoFillBackground(true);
    setPalette(QPalette(Qt::white));
    resize(180,180);
    setMinimumSize(100,100);
    mouseX=width()/2;
    mouseY=height()/2;
    tim=new QTimer(this);
    connect(tim,&QTimer::timeout,this,[=]{
       emit keyNumchanged(getKeyNum());
   });
    connect(this,&Handle::keyNumchanged,this,[=]{
        emit chassis_move_ctrl();
    });
}

Handle::~Handle()
{

}
//绘制图像部分
void Handle::paintEvent(QPaintEvent *){

    QPainter painter(this);

    int side = qMin(width(), height());

    padR=side/2; //底盘半径
    padX=padR;//底盘圆心
    padY=padR;//底盘圆心
    handleR=padR/2;//摇杆圆半径
    int handleMaxR=padR-handleR;
    QColor handleColor(Qt::gray);

    //自绘底盘
    painter.save();
    QRadialGradient RadialGradient(padR,padR,padR*3,padR,padR);//圆心2，半径1，焦点2
    RadialGradient.setColorAt(0,QColor(90,90,90,127));//渐变
    RadialGradient.setColorAt(1,QColor(255,255,255,190));//渐变
    painter.setBrush(RadialGradient);
    painter.setPen(Qt::NoPen);
    painter.drawEllipse(QPoint(padR,padR),side/2,side/2);//大圆盘
    painter.restore();

//    painter.drawText(20,20,tr("%1,%2,%3").arg(mouseX).arg(mouseY).arg(handPaddis));

    if(!mousePressed){//鼠标没按下则摇杆恢复到底盘中心
        mouseX=padX;
        mouseY=padY;
    }
    handPadDis=Pointdis(padR,padR,mouseX,mouseY);
    if(handPadDis<=handleMaxR){
        handleX=mouseX;
        handleY=mouseY;
    }
    else {
        handleX=(int)(handleMaxR*(mouseX-padX)/handPadDis+padX);
        handleY=(int)(handleMaxR*(mouseY-padY)/handPadDis+padY);
    }

    painter.setPen(Qt::NoPen);
    painter.setBrush(handleColor);
    painter.drawEllipse(QPoint(handleX,handleY),handleR,handleR);//摇杆
}


//信号部分
void Handle::mouseMoveEvent(QMouseEvent* event){
    static bool r=false;
    mouseX=event->pos().x();
    mouseY=event->pos().y();
    if(r==true){
       update();
       r=false;
    }
    else{
        r=true;
    }
}
void Handle::mouseReleaseEvent(QMouseEvent* event){
    mouseX=width()/2;
    mouseY=height()/2;
    tim->stop();
    mousePressed=false;

    update();
}
void Handle::mousePressEvent(QMouseEvent* event){
    mouseX=event->pos().x();
    mouseY=event->pos().y();
    tim->start(100);
    mousePressed=true;
    emit chassis_move_ctrl();
    update();
}

double Handle::Pointdis(int a,int b,int x,int y){
    return sqrt((double)((x-a)*(x-a)+(y-b)*(y-b)));
}
float Handle::getKeyNum()
{
    int x,y;
    int keynum;
    int keyangle;
    int x_zero , y_zero ;
//    x=(int)(handleX*3.0/(padR*2));
//    y=(int)(handleY*3.0/(padR*2));
//    keynum=3*y+x;
    if(left_right_ctrl == 1)//左边的摇杆
    {
        x_zero = 90;
        y_zero = 90;
        x = handleX;
        y = handleY;
        float keyrad = atan2((y - y_zero),(x - x_zero));
       // std::cout << keyrad << std::endl;
        keyrad = -keyrad;
        return keyrad;
    }
    else if(left_right_ctrl == 2)
    {
        x_zero = 90 ;
        //y_zero = 700  ;
        x = handleX;
        //y = handleY;
        float key_yaw_ctrl ;
        if(x > x_zero)
        {
            key_yaw_ctrl = 1;
           // std::cout << key_yaw_ctrl<<std::endl;
            return key_yaw_ctrl;
        }
        else if(x<x_zero)
        {
            key_yaw_ctrl = 0;
            //std::cout << key_yaw_ctrl <<std::endl;
            return key_yaw_ctrl;
        }
    }

}
void Handle::handle_signal()
{
    //发送信号
    emit keyNumchanged(Handle::stop);
}


